{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 贪心法\n",
    "from typing import List\n",
    "\n",
    "\n",
    "\n",
    "def maxSubArray(nums: List[int]) -> int:\n",
    "    if not nums:\n",
    "        return None\n",
    "\n",
    "    current_sum = max_sum = nums[0]\n",
    "\n",
    "    for i in range(1, len(nums)):\n",
    "        # current_sum = max(current_sum, current_sum+nums[i]) # ?\n",
    "        # 若当前指针所指元素之前的和小于0，则丢弃当前元素之前的数列\n",
    "        # current_sum<0 则current_sum+nums[i]<nums[i]\n",
    "        current_sum = max(nums[i], current_sum + nums[i])\n",
    "        max_sum = max(max_sum, current_sum)\n",
    "\n",
    "    return max_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 动态规划\n",
    "def maxSubArray(nums: List[int]) -> int:\n",
    "    if not nums:\n",
    "        return None\n",
    "\n",
    "    for i in range(1, len(nums)):\n",
    "        # 若前一个元素大于0，则将其加到当前元素上\n",
    "        if nums[i-1]>0:\n",
    "            nums[i]+=nums[i-1]\n",
    "    \n",
    "    return max(nums)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
